﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Model.Result.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-09-15 16:11
    /// 描 述：服务类
    /// </summary>
    public class StoreGoodsProductService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<StoreGoodsProductEntity>> GetList(StoreGoodsProductListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<StoreGoodsProductEntity>> GetPageList(StoreGoodsProductListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<StoreGoodsProductEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<StoreGoodsProductEntity>(id);
        }

        public List<goodscar> GetSelectCarListJson(StoreGoodsProductListParam param)
        {
            var expression = ListFilter(param);

            var list = this.BaseRepository().FindList(expression).Result;

            List<goodscar> result = new List<goodscar>();

            foreach (var item in list)
            {
                goodscar dto = new goodscar();
                dto.pw_id = item.Id.Value;
                dto.VINName = item.VIN + "[" + item.name + "]";

                result.Add(dto);
            }
            return result;
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(StoreGoodsProductEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<StoreGoodsProductEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<StoreGoodsProductEntity, bool>> ListFilter(StoreGoodsProductListParam param)
        {
            var expression = LinqExtensions.True<StoreGoodsProductEntity>();
            if (param != null)
            {
                if (!param.shopid.IsNullOrZero() && param.shopid != -1)
                {
                    expression = expression.And(t => t.shop_id == param.shopid);
                }
                if (!param.colorid.IsNullOrZero())
                {
                    expression = expression.And(t => t.colorid == param.colorid);
                }
                if (!param.name.IsEmpty())
                {
                    expression=expression.And(t => t.name.Contains(param.name));
                }
                if (!param.goodsid.IsNullOrZero() && param.goodsid != -1)
                {
                    expression = expression.And(t => t.goods_id== param.goodsid);
                }
                if (!param.color.IsEmpty())
                {
                    expression = expression.And(t => t.color.Contains(param.color));
                }
                if (!param.goods_name.IsEmpty())
                {
                    expression = expression.And(t => t.goods_name.Contains(param.goods_name));
                }
            }
            return expression;
        }
        #endregion
    }
}
